package com.yuanqunsoft.alimigration.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.yuanqunsoft.alimigration.dao.IRuleDao;
import com.yuanqunsoft.alimigration.model.Alert;
import com.yuanqunsoft.alimigration.model.Remediation;
import com.yuanqunsoft.alimigration.model.Rule;
import com.yuanqunsoft.alimigration.model.RuleImplement;
import com.yuanqunsoft.alimigration.model.RulesetWithRule;

public class RuleDaoImpl extends HibernateDaoSupport implements IRuleDao {

    private int currentPage;
    private int allPage;
    private int pageSize;

    @Override
    public boolean save(Rule rule) {
        // TODO Auto-generated method stub
        try {
            this.getHibernateTemplate().save(rule);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean update(Rule rule) {
        // TODO Auto-generated method stub
        try {
            this.getHibernateTemplate().update(rule);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean delete(Rule rule) {
        // TODO Auto-generated method stub
        try {

            String hql = "from RulesetWithRule r where r.rule.rulId=?";
            List<RulesetWithRule> rs = this.getHibernateTemplate().find(hql, rule.getRulId());
            for (int i = 0; i < rs.size(); i++) {
                this.getHibernateTemplate().delete(rs.get(i));
            }
            String hqla = "from Alert a where a.rule.rulId=?";
            List<Alert> alerts = this.getHibernateTemplate().find(hqla, rule.getRulId());
            Alert alert;
            Rule r = new Rule();
            for (int i = 0; i < alerts.size(); i++) {
                alert = alerts.get(0);
                this.getHibernateTemplate().delete(alert);
                alert.setRule(r);
                this.getHibernateTemplate().save(alert);
            }
            String hals = "from RuleImplement r where r.rule.rulId=?";
            List<RuleImplement> rls = this.getHibernateTemplate().find(hals, rule.getRulId());
            for (int i = 0; i < rls.size(); i++) {
                this.getHibernateTemplate().delete(rls.get(0));
            }
            Remediation rmd;
            String hqls = "from Remediation r where r.rule.rulId=?";
            List<Remediation> rm = this.getHibernateTemplate().find(hqls, rule.getRulId());
            for (int i = 0; i < rm.size(); i++) {
                rmd = rm.get(0);
                this.getHibernateTemplate().delete(rmd);
                rmd.setRule(r);
                this.getHibernateTemplate().save(rmd);
            }
            this.getHibernateTemplate().delete(rule);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public List<Rule> getAllRules() {
        // TODO Auto-generated method stub
        String hql = "from Rule";
        return this.getHibernateTemplate().find(hql);
    }

    @Override
    public List<Rule> queryPage(int currentPage, int pageSize) {
        // TODO Auto-generated method stub
        this.currentPage=currentPage;
        this.pageSize=pageSize;
        return (List<Rule>) this.getHibernateTemplate().execute(new HibernateCallback() {

            @Override
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                // TODO Auto-generated method stub
                String hql = "from Rule";
                Query q = session.createQuery(hql);
                List<Rule> r1 = q.list();
                allPage = r1.size();
                q = session.createQuery(hql);
                q.setMaxResults(RuleDaoImpl.this.pageSize);
                q.setFirstResult(RuleDaoImpl.this.currentPage);
                return q.list();
            }
        });
    }

    @Override
    public int getAllPage() {
        // TODO Auto-generated method stub
        return this.allPage;
    }

    @Override
    public Rule getRuleById(int id) {
        // TODO Auto-generated method stub
        return this.getHibernateTemplate().get(Rule.class, id);
    }

    @Override
    public Rule getRuleByName(String name) {
        String hql = "from Rule where rulName=?";
        List<Rule> rule = this.getHibernateTemplate().find(hql, name);
        return (rule == null || rule.size() == 0) ? null : rule.get(0);
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setAllPage(int allPage) {
        this.allPage = allPage;
    }

}
